package com.chenxingnet.service.Impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;














import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.chenxingnet.dao.RoleMapper;
import com.chenxingnet.dao.UserDepartmentMapper;
import com.chenxingnet.dao.UsercontrolMapper;
import com.chenxingnet.dto.Role;
import com.chenxingnet.dto.UserDepartment;
import com.chenxingnet.dto.Usercontrol;
import com.chenxingnet.service.ManagerService;
import com.chenxingnet.tools.Page;
@Service("managerService")
public class ManagerServiceImpl implements ManagerService{
	@Resource
	private UsercontrolMapper usercontrolMapper;

	@Resource
	private UserDepartmentMapper   userDepartmentMapper;
	
	@Resource
	private RoleMapper roleMapper;
	
	public Usercontrol userLogin(String uAccount, String uPassword) {
		
		return usercontrolMapper.userLogin(uAccount, uPassword);
	}

	
	@Transactional(rollbackFor=Throwable.class)
	public int userAdd(Usercontrol usercontrol,int  depid) {
		int    res=usercontrolMapper.insertSelectiveMy(usercontrol);
		UserDepartment   userDepartment=new  UserDepartment();
		userDepartment.setDeptid(depid);
		userDepartment.setUdreportstart("1");
		userDepartment.setUid(usercontrol.getUid());
		res=	userDepartmentMapper.insertSelective(userDepartment);
		return res;
	}


	public Role selectByRid(Integer rid) {
		
		return roleMapper.selectByPrimaryKey(rid);
	}


	public int updatePwd(Usercontrol user) {
		return usercontrolMapper.updatePwd(user);
		
	}


	public List<Usercontrol> userTest(String username) {
		return usercontrolMapper.userTest(username);
	}


	public void showUserByCount(HttpServletRequest request, Model model) {
		String pageNow = request.getParameter("pageNow");
		Page page = null;
		List<Usercontrol> userList = new ArrayList<Usercontrol>();
		int count = (int)usercontrolMapper.getCount();
		if(pageNow != null){
			page = new Page(count,Integer.valueOf(pageNow));
			userList = usercontrolMapper.showUser(page.getStartPos(), page.getPageSize());
		}else{
			page = new Page(count,1);
			userList = usercontrolMapper.showUser(page.getStartPos(), page.getPageSize());
		}
		model.addAttribute("page", page);
		model.addAttribute("userList", userList);
		
	}


	public void delUser(Integer uid) {
		int uid2 = userDepartmentMapper.delByUid(uid);
		System.out.println("userDept连接表删除"+uid2+"条数据");
		int key = usercontrolMapper.deleteByPrimaryKey(uid);
		System.out.println("user表删除"+key+"条数据");
	}
	
	

}
